<?php
class MySQL {
	public function connect($host, $user, $pass, $database) {
			$link = mysql_connect($host, $user, $pass) or die('Connect mysql fail');
        	$db_selected = mysql_select_db($database, $link) or die('Select database fail');
			mysql_query("SET NAMES 'utf8'");
			return $link;
    }
    public function query($query, $connect) {
        	$result = mysql_query($query, $connect) or die('execute sql command fail');
    		return $result;
	}
	public function query_row($query, $connect) {
        	$result = mysql_query($query, $connect) or die('execute sql command fail');
			$row = mysql_fetch_row($result);
			return $row;
	}
	public function real($word) {
		return mysql_real_escape_string($word);
	}
	/**
	 * Storing new user
	 * returns user details
	 */
	public function storeUser($name, $phone, $gcm_regid, $imsi) {
		// insert user into database
		$result = mysql_query("INSERT INTO gcm_users(name, phone, gcm_regid, created_at, imsi) VALUES('$name', '$phone', '$gcm_regid', NOW()), $imsi");
		// check for successful store
		if ($result) {
			// get user details
			$id = mysql_insert_id(); // last inserted id
			$result = mysql_query("SELECT * FROM gcm_users WHERE id = $id") or die(mysql_error());
			// return user details
			if (mysql_num_rows($result) > 0) {
				return mysql_fetch_array($result);
			} else {
				return false;
			}
		} else {
			return false;
		}
	}
	
	/**
	 * Get user by email and password
	 */
	public function getUserByEmail($email) {
		$result = mysql_query("SELECT * FROM gcm_users WHERE email = '$email' LIMIT 1");
		return $result;
	}
	
	/**
	 * Getting all users
	 */
	public function getAllUsers() {
		$result = mysql_query("select * FROM gcm_users");
		return $result;
	}
	
	/**
	 * Check user is existed or not
	 */
	public function isUserExisted($email) {
		$result = mysql_query("SELECT email from gcm_users WHERE email = '$email'");
		$no_of_rows = mysql_num_rows($result);
		if ($no_of_rows > 0) {
			// user existed
			return true;
		} else {
			// user not existed
			return false;
		}
	}
}
?>
